package com.lyc.mapper;

import com.lyc.DTO.PostDTO;
import com.lyc.pojo.Category;
import com.lyc.pojo.ItemType;
import com.lyc.pojo.Post;
import com.lyc.pojo.PostImages;
import org.apache.ibatis.annotations.*;

import java.util.Collection;
import java.util.List;

@Mapper
public interface PostMapper {

    // 添加帖子
    @Insert("insert into posts " +
            "( user_id, category_id,title, item_type_id, " +
            "content, contact_info," +
            " price, location, is_urgent, " +
            "expired_at, created_at)" +
            " VALUES " +
            "(#{userId}, #{categoryId}, #{title}, #{itemTypeId}, " +
            "#{content}, #{contactInfo}," +
            " #{price}, #{location}, #{isUrgent}," +
            " #{expiredAt}, now())")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    void insert(Post post);

    // 获取所有帖子
    @Select("select * from posts")
    List<Post> getAllPosts();

    // 根据用户ID获取用户帖子
    @Select("select * from posts where user_id = #{userId}")
    List<Post> getPostsByUserId(Integer userId);

    // 添加帖子图片
    @Insert("insert into post_images (post_id, image_url, sort_order, created_at) " +
            "VALUES (#{postId}, #{imageUrl}, #{sortOrder}, now())")
    void insertPostImages(PostImages postImage);

    // 获取当前用户帖子信息
    @Select("select * from posts where user_id = #{userId}")
    List<Post> getUserPostInfo(Integer userId);

    // 获取帖子详情
    @Select("select * from posts where id = #{id}")
    Post getPostDetail(Integer id);

    // 增加帖子浏览量
    @Update("UPDATE posts SET view_count = view_count + 1 WHERE id = #{id}")
    void incrementViewCount(@Param("id") Integer id);
    
    // 根据postId获取实习/兼职信息ID
    @Select("SELECT id FROM internship_jobs WHERE post_id = #{postId}")
    Integer getInternshipJobIdByPostId(Integer postId);

    // 获取所有分类
    @Select("select * from categories")
    List<Category> getCategories();

    // 获取贴子封面
    @Select("select * from post_images")
    List<PostImages> getCover();

    // 根据帖子ID获取封面图片
    @Select("select * from post_images where post_id = #{postId}")
    List<PostImages> getCoverById(Integer postId);

    // 根据ID获取单个帖子图片
    @Select("select * from post_images where id = #{id}")
    PostImages getPostImageById(Integer id);

    // 获取二手/失物帖子类型
    @Select("select * from item_types where category_id = #{categoryId} order by id asc")
    List<ItemType> getItemTypes(Integer categoryId);

    // 获取二手/失物帖子列表--分页
    @Select("select * from posts where category_id = #{categoryId}")
    List<Post> getItemList(Integer categoryId);

    // 获取二手/失物帖子列表--非分页
    @Select("select * from posts where category_id = #{categoryId}")
    List<Post> getItemLists(Integer categoryId);

    // 按浏览量降序排序
    @Select("select * from posts order by view_count desc")
    List<Post> selectAllOrderByViewCountDesc();

    // 更新帖子封面--阿里云
//    @Update("update post_images set image_url = #{imageUrl} where post_id = #{postId}")
//    void updateCover(String imageUrl, Integer postId);

     //更新帖子封面--本地存储
    @Update("update post_images set image_url = #{imageUrl} where post_id = #{postId}")
    void updateCover(String imageUrl, Integer postId);

    // 更新用户帖子
    @Update("update posts set " +
            "title = #{title},category_id=#{categoryId}, item_type_id=#{itemTypeId}, " +
            "content = #{content}, contact_info = #{contactInfo}, " +
            "price = #{price}," +
            "location = #{location}, is_urgent = #{isUrgent}, " +
            "expired_at = #{expiredAt}, updated_at = now() " +
            "where id = #{id}")
    void update(PostDTO postDTO);

    // 获取热门帖子
    @Select("select * from posts order by view_count desc limit 5")
    List<Post> getHotPosts();

    // 根据帖子ID删除图片记录
    @Delete("delete from post_images where post_id = #{postId}")
    void deletePostImagesByPostId(Integer postId);
    
    // 更新帖子状态
    @Update("update posts set status = #{status} where id = #{id}")
    void updatePostStatus(Post post);

    // 删除帖子封面
    @Delete("delete from post_images where id = #{id}")
    void deleteCover(Integer id);

    // 删除用户帖子
    @Delete("delete from posts where id = #{id}")
    void deletePost(Integer id);
}